package com.bringway.p2p.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.BankcardService;
import com.bringway.p2p.service.P2PUserService;

/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月14日 上午9:34:42
 * @功能：抽奖结果页面
 */


@Controller
@RequestMapping("/bankcard")
public class BankcardController extends JsonController{
	
	@Autowired
	private BankcardService bankcardService;
	
	@RequestMapping("/queryBankcard")
	/**
	 * 
	 * @param 返回数据结果集
	 * @param response 相应请求懂啊客户端
	 * @return 返回查询的数据
	 * @throws Exception
	 */
	public ModelAndView queryCjjgList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String userid = RequestUtils.getStringParameter(request, "userid");
		String username = RequestUtils.getStringParameter(request, "username");
		String cardid = RequestUtils.getStringParameter(request, "cardid");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String sfzhm = RequestUtils.getStringParameter(request, "sfzhm");
		String tjsj = RequestUtils.getStringParameter(request, "tjsj");

		
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("userid", userid);
		params.put("username", username);
		params.put("sjhm", sjhm);
		params.put("cardid", cardid);
		params.put("sfzhm", sfzhm);
		params.put("tjsj", tjsj);
		List list = bankcardService.querybakcard(params);
		int count = bankcardService.getbankcardCount(params);
		result.put("data", list);	
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request,response,result);
		return null;
	}
	
	
	
	/**
	 * @方法名称:addBnakcard
	 * @方法描述:添加银行卡信息
	 * @author qjy
	 * @date 2016年07月13日下午3:28:11
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addBnakcard")
	public ModelAndView addBankcard(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = RequestUtils.getStringParameter(request, "id");
		String userid = RequestUtils.getStringParameter(request, "userid");
		String cid = RequestUtils.getStringParameter(request, "cid");
		String name = RequestUtils.getStringParameter(request, "name");
		String sfz = RequestUtils.getStringParameter(request, "sfz");
		String yhmc = RequestUtils.getStringParameter(request, "yhmc");
		String khzd = RequestUtils.getStringParameter(request, "khzd");
		String khh = RequestUtils.getStringParameter(request, "khh");
		JSONObject parms = new JSONObject();
		JSONObject result = new JSONObject();
		parms.put("id", id);
		parms.put("userid", userid);
		parms.put("cid", cid);
		parms.put("name", name);
		parms.put("sfz", sfz);
		parms.put("yhmc", yhmc);
		parms.put("khzd", khzd);
		parms.put("khh", khh);
		boolean isCunZai= bankcardService.queryBankcardid(parms);
		if (isCunZai) {
			result.put("success", true);
			result.put("isAdd", false);
			result.put("msg", "银行卡已存在!");
			serialize(request, response, result);
		}else {
			SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			parms.put("OPER_USERID", sysUserDetails.getGyh());
			boolean bool = bankcardService.addBankcard(parms);
			result.put("success", true);
			result.put("isAdd", bool);
			serialize(request, response, result);
		}
		return null;
	}
	
	
	
	/**
	 * @方法名称: queryHdbJkrBankcardList
	 * @方法描述: 红点宝-借款人-银行卡信息查询
	 * @param 返回数据结果集
	 * @param response 相应请求懂啊客户端
	 * @return 返回查询的数据
	 * @throws Exception
	 */
	@RequestMapping("/queryHdbJkrBankcardList")
	public ModelAndView queryHdbJkrBankcardList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String userid = RequestUtils.getStringParameter(request, "userid");
		String username = RequestUtils.getStringParameter(request, "username");
		String cardid = RequestUtils.getStringParameter(request, "cardid");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String sfzhm = RequestUtils.getStringParameter(request, "sfzhm");
		String tjsj = RequestUtils.getStringParameter(request, "tjsj");

		
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("userid", userid);
		params.put("username", username);
		params.put("sjhm", sjhm);
		params.put("cardid", cardid);
		params.put("sfzhm", sfzhm);
		params.put("tjsj", tjsj);
		List hdbJkrList = bankcardService.queryHdhJkrList(params);
		if(hdbJkrList.size() > 0){
			List list = bankcardService.queryHdbJrkBankcardList(hdbJkrList,params);
			int count = bankcardService.getHdbJrkBankcardCount(hdbJkrList,params);
			result.put("data", list);	
			result.put("total", count);
		}else{
			result.put("data", null);	
			result.put("total", 0);
		}
		serialize(request,response,result);
		return null;
	}
	
	/**
	 * @方法名称:queryHdbJkrListAll
	 * @方法描述:获取红点宝借款人信息列表
	 * @author xuef
	 * @date 2017年3月7日15:07:20
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryHdbJkrListAll")
	public ModelAndView queryHdbJkrListAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		List hdbJkrList = bankcardService.queryHdhJkrList(params);
		result.put("data", hdbJkrList);
		serialize(request, response, result);
		return null;
	}
	
	/**
	 * @方法名称:queryHdbJkrListAll
	 * @方法描述:获取红点宝借款人信息列表
	 * @author xuef
	 * @date 2017年3月7日15:07:25
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getHdbJkrUserInfo")
	public ModelAndView getHdbJkrUserInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		String userId = RequestUtils.getStringParameter(request, "userId");
		Map hdbJkrMap = bankcardService.getHdbJkrUserInfo(userId, "HDB_JRK");
		result.put("success", true);
		result.put("data", hdbJkrMap);
		serialize(request, response, result);
		return null;
	}
	
	
	/**
	 * @方法名称:resetPassword
	 * @方法描述:重置密码
	 * @author fan
	 * @date 2015年12月29日下午4:27:57
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delBankcardInfo")
	@Transactional("transactionManager_p2p")
	public ModelAndView delBankcardInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String id = RequestUtils.getStringParameter(request, "id");
		String cardId = RequestUtils.getStringParameter(request, "cardId");
		params.put("id", id);
		params.put("cardId", cardId);
		Map isExsitBankcardInfo = bankcardService.queryBankcardInfo(params);
		if(null == isExsitBankcardInfo){
			result.put("success", false);
			result.put("msg", "该信息不存在！");
		}else{
			boolean isDelSuc = bankcardService.delBankcardInfo(params);
			if(isDelSuc){
				SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
				isExsitBankcardInfo.put("JB_OPER_USERID", sysUserDetails.getGyh());
				this.bankcardService.saveBankcardBk(isExsitBankcardInfo);
				result.put("success", true);
				result.put("msg", "操作成功！");
			}else{
				result.put("success", false);
				result.put("msg", "操作失败！");
			}
		}
		serialize(request, response, result);
		return null;
	}
}
